﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BCDataCenter;
using BCDataCenter.BOL;
using Data;

public partial class _Default : System.Web.UI.Page
{
    string error = "";
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    BCDataCenter.ClassWriteLogErr objk;

    clsObj Obj;
    clsSql Sql = new clsSql();

    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Clear();
    }
    static public string GetMd5Sum(string str)
    {
        // First we need to convert the string into bytes, which
        // means using a text encoder.

        Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
        // Create a buffer large enough to hold the string

        byte[] unicodeText = new byte[str.Length * 2];

        enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);

        // Now that we have a byte array we can ask the CSP to hash it

        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] result = md5.ComputeHash(unicodeText);

        // Build the final string by converting each byte

        // into hex and appending it to a StringBuilder

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < result.Length; i++)
        {

            sb.Append(result[i].ToString("X2"));

        }
        // And return it
        return sb.ToString();
    }
    protected void btLogin_OnClick(object sender, EventArgs e)
    {
        string pass = GetMd5Sum(txtPassword.Text);
        dt = new ClassExecute.ClassExecute();

        DataTable db = dt.Get_Data_DataTable(str, ref error, "getUserLogon", CommandType.StoredProcedure,
            new SqlParameter("@UserName", txtUsername.Text),
            new SqlParameter("@Password", pass));

        if (db.Rows.Count>0)
        {
            Session["username"] = db.Rows[0]["UserName"].ToString();
            Session["Sitecode"] = db.Rows[0]["Sitecode"].ToString();
            Session["UserType"] = db.Rows[0]["UserType"].ToString();
            CheckLoadUrl(Session["UserType"].ToString());
            Response.Redirect(Globals.ApplicationPath + "WebMaster/Default.aspx");
        }
        else
            {
                DataTable ds =dt.Get_Data_DataTable(str,ref error,"Tr_PubgetUserLogOnViewReport",CommandType.StoredProcedure,
                    new SqlParameter("@MaTVV",txtUsername.Text),
                    new SqlParameter("@Pass",txtPassword.Text));
               
                    if (ds.Rows.Count > 0)
                    {
                        Session["username"] = "DefaultUser";
                        Session["Salesp_key_Logon"] = txtUsername.Text;
                        Session["Sitecode"] = ds.Rows[0]["BelongTo"].ToString();
                        Session["UserType"]="enduser";
                        CheckLoadUrl(Session["UserType"].ToString());
                        Response.Redirect(Globals.ApplicationPath + "WebMaster/Default.aspx");
                      

                    }
                    else
                    {
                        uscMsgBox1.AddMessage("Tên đăng nhập hoặc mật khẩu không hợp lệ", uscMsgBox.enmMessageType.Error);
                    }
               
             }
        objk = new BCDataCenter.ClassWriteLogErr();
        objk.LoginLog(txtUsername.Text, txtIP.Text);
        
     }
    void CheckLoadUrl(string UserType)
    {
        if (UserType != "ad")
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[k_getPageUser]", CommandType.StoredProcedure,
                new SqlParameter("@UserName", Session["username"].ToString()));
            Session["PageUrl"] = db;
        }
    }
}
